﻿using Microsoft.Toolkit.Uwp.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{

    [LastModified("2022-5-12", "create PictureToHuePage.xaml")]
    [PageType("cpu", "图片色相")]

    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class PictureToHuePage : Page
    {
        ObservableCollection<ColorInfo> obs = new ObservableCollection<ColorInfo>();

        public PictureToHuePage()
        {
            this.InitializeComponent();
        }

        private async void selectPicture_Click(object sender, RoutedEventArgs e)
        {
            var bitmap = await Function.SelectImageAsync();
            obs.Clear();
            var colors = bitmap.GetPixelColors();
            var tempColor = new Color[colors.Length];
            var list =new List<Hsv>();
            var index =0;
            foreach (var item in colors)
            {
                var chsv = Microsoft.Toolkit.Uwp.Helpers.ColorHelper.ToHsv(item);
                var chsl = Microsoft.Toolkit.Uwp.Helpers.ColorHelper.ToHsl(item);
                if (chsv.H == 60)
                {
                    var i = 1;
                }
                var hsv = Function.RgbToHsv(item);
                tempColor[index] = hsv.HsvToRgb();
                index++;
               
                var tempHsv = list.Find((temp) => { return temp.H == hsv.H; });
                if(tempHsv == null)
                {
                    list.Add(hsv);
                }
            }

            list.Sort((a, b) =>
            {
                return (int)a.H - (int)b.H;
            });

            foreach (var item in list)
            {
                //item.S = 100;
                //item.V = 100;
                var color= item.HsvToRgb();
                var info = new ColorInfo();
                info.HSV = item;
                info.Color = color;
                info.ColorName = color.ToString();
                info.ColorBrush = new SolidColorBrush(color);
                obs.Add(info); 
            }


            Function.InitialImage(tempColor, (int)bitmap.Size.Width, (int)bitmap.Size.Height, image);
            //Function.InitialImage(bitmap, image);
        }
    }
}
